<template>
  <div>
    <Spin :spinning="loading" tip="Loading...">
      <div style="padding-top: 1rem"></div>
    <!-- table -->
    <!-- 经销商配件管理费率维护及工时系数维护 - 特殊配件管理费率新增 -->
    <FormPanel ref="formPanel1" :list="formList" :cols="1" :formType="'add'" labelWidth="110" :isSubmitBtn="false" @formChange="formChangeHandle" />

    <div style="padding-top: 1rem"></div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right',
      }"
    >
      <multiuse-button type="primary" :click="saveDialog.bind()">保 存</multiuse-button>
      <multiuse-button @click="closeDialog">关 闭</multiuse-button>
    </div>
    <BaseDialog :visible.sync="visible" :title="dialogTitle" destroyOnClose width="850px" :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <DealerList @close="close" @save="save"></DealerList>
    </BaseDialog>
    </Spin>
  </div>
</template>
<script>
import DealerList from '@/pages/claimDemage/tools/DealerList.vue';
import { newSave } from '@/api/claim/factory/DealerPartManageHourMaintain';
import { selectByParaCode, multiples } from '@/api/claim/factory/SystemParameterMaintain';
import { getAppId} from '@/assets/js/auth';
import {dictionary} from'@/utils/dictMixin';
export default {
  mixins: [dictionary],
  components: {
    DealerList,
  },
  data() {
    return {
      loading: false,
      dialogTitle: '选择经销商',
      visible: false,
      visibleDrawer: false,
      collapse: false,
      fetchParams: {}, //ipa 接口
      selectedRows: [], //选中经销商
      formList: this.createFormList(),
      saveData: null,
    };
  },
  created() {
    this.chuqi();
    if(getAppId()=='beijing'){
      this.formList.find((x) => x.fieldName == 'powerType').itemList = this.createDictList('6008');
    }else{
      this.formList.find((x) => x.fieldName == 'powerType').itemList = this.createDictList('6008');
      this.formList.find((x) => x.fieldName == 'powerType').initialValue = 60081002;
      this.formList.find((x) => x.fieldName == 'powerType').disabled=true;
    }
  },
  methods: {
    async chuqi() {
      const res = await selectByParaCode({ paraCode: 'XTCS00007' });
      if (res.resultCode == 200) {
        this.formList.find((x) => x.fieldName == 'normalPrice').initialValue = res.data.para1;
        this.para1 = res.data.para1;
      }
    },
    async multiple(val) {
      this.formList.find((x) => x.fieldName == 'mngLab').initialValue = parseFloat((val/ this.para1)*100).toFixed(2);
      // const res = await multiples({ A: this.para1, B: val });
      // if (res.resultCode == 200) {
      //   this.formList.find((x) => x.fieldName == 'labPrice').initialValue = res.data;
      // }
    },
    formChangeHandle(val) {
      console.log(val);
      this.saveData = val;
    },
    //搜索获取数据
    changeHandle(val) {
      this.fetchParams = val;
      console.log(val);
    },
    //选中
    selectChange(rows) {
      this.selectedRows = rows;
      console.log(this.selectedRows);
    },
    //保存
    async saveDialog() {
      this.$refs.formPanel1.SUBMIT_FORM();
      if (this.saveData !== null) {
        this.loading = true;
        let res = await newSave(this.saveData);
        console.log(res);
        if (res.resultCode === 200) {
          this.$emit('save');
          return this.$notify.success({ title: '提示信息', message: '保存成功' });
        } else {
          this.loading = false;
          // this.$notify.error({ title: '提示信息', message: res.errMsg || '保存失败!' });
          // this.closeDialog();
        }
      }
    },
    //取消操作
    closeDialog() {
      this.$emit('close', { visible: false, data: this.selectedRows, type: 'close' });
    },
    chooseOrderNo() {
      this.visible = true;
    },
    close() {
      this.visible = false;
    },
    save(obj) {
      this.formList.find((x) => x.fieldName == 'dealerCode').initialValue = obj[0].dealerCode;
      this.formList.find((x) => x.fieldName == 'dealerName').initialValue = obj[0].dealerName;
      this.visible = false;
    },
    createFormList() {
      return [
        {
          type: 'INPUT',
          label: '经销商代码',
          fieldName: 'dealerCode',
          placeholder: '请选择经销商代码...',
          readonly: true, // 只读
          rules: [{ required: true, message: '请选择经销商代码...', trigger: 'change' }],
          unitRender: (p) => {
            return (
              <div>
                <multiuse-button icon="el-icon-search" onClick={() => this.chooseOrderNo()} />
              </div>
            );
          },
        },
        {
          type: 'INPUT',
          label: '经销商名称',
          fieldName: 'dealerName',
          rules: [{ required: true, message: '请输入经销商名称...', trigger: 'change' }],
          readonly: true, // 只读
          disabled: true, // 置会
        },
        {
          type: 'INPUT',
          label: '配件管理费率',
          fieldName: 'spMngRate',
          placeholder: '请输入配件管理费率...',
          rules: [{ required: true,  message: '请输入配件管理费率...', trigger: 'blur' }, {pattern: /^(\d+)(\.?)(\d{0,2})$/,message:'请输入数字值,小数点后两位...',trigger: 'blur'}],
          unitRender: () => {
            return <span>(%)</span>;
          },
        },
        {
          type: 'INPUT',
          label: '工时系数',
          fieldName: 'mngLab',
          placeholder: '请输入工时系数',
          disabled: true,
          // rules: [
          //   { required: true, message: '请输入工时系数', trigger: 'change' },
          //   {
          //     validator: (rule, value, callback) => {
          //       let reg = /^(\d+)(\.?)(\d{0,2})$/;
          //       if (value < 100) {
          //         return callback(new Error('工时系数,最小为100%'));
          //       }
          //       if(!reg.test(Number(value))){
          //         return callback(new Error('工时系数,最多2位小数'));
          //       }
          //       callback();
          //     },
          //     trigger: 'change',
          //   }
          // ],
          unitRender: (p) => {
            return <div>%</div>;
          },
          // change: (val) => {
          //   this.multiple(val);
          // },
        },
        {
          type: 'INPUT',
          label: '工时单价',
          fieldName: 'labPrice',
          placeholder: '请输入工时单价',
          // disabled: true,
          unitRender: (p) => {
            return <div>元</div>;
          },
          rules: [
            { required: true, message: '请输入工时单价', trigger: 'change' },
            {
              validator: (rule, value, callback) => {
                let reg = /^(\d+)(\.?)(\d{0,2})$/;
                if (value < 0) {
                  return callback(new Error('工时单价,最小为0'));
                }
                if(!reg.test(Number(value))){
                  return callback(new Error('工时单价,最多2位小数'));
                }
                callback();
              },
              trigger: 'change',
            }
          ],
          change: (val) => {
            this.multiple(val);
          },
        },

        {
          type: 'INPUT',
          label: '标准工时单价',
          fieldName: 'normalPrice',
          placeholder: '请输入标准工时单价',
          disabled: true,
          unitRender: (p) => {
            return <div>元</div>;
          },
        },
        {
          type: 'INPUT',
          label: '配件最大单价',
          fieldName: 'maxPrice',
          placeholder: '请输入配件最大单价',
          // disabled: true,
          unitRender: (p) => {
            return <div>元</div>;
          },
          rules: [
            { required: true, message: '请输入配件最大单价', trigger: 'change' },
            {
              validator: (rule, value, callback) => {
                let reg = /^(\d+)(\.?)(\d{0,2})$/;
                if (value < 0) {
                  return callback(new Error('配件最大单价,最小为0'));
                }
                if(!reg.test(Number(value))){
                  return callback(new Error('配件最大单价,最多2位小数'));
                }
                callback();
              },
              trigger: 'change',
            }
          ],
        },
        {
          type: 'SELECT',
          label: '动力形式',
          fieldName: 'powerType',
          placeholder: '请选择动力形式...',
          initialValue: '',
          itemList:[],
          rules: [{required: true, message: '请选择动力形式...', trigger: 'change'}]
          },
        {
          type: 'SELECT',
          label: '状态',
          fieldName: 'labStatus',
          placeholder: '请选择状态',
          filterable: true,
          initialValue:1,
          itemList: [
            { text: '启用', value: 1 },
            { text: '禁用', value: 0 },
          ],
          rules: [{ required: true, message: '请选择状态', trigger: 'change' }],
        },
      ];
    }
  },
};
</script>

<style lang="less" scoped></style>
